/*
 * Copyright (C) 2011 by Fabien Barbero
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.flickr.desktop.gui.views.comps;

import com.flickr.api.Paginated;
import com.flickr.desktop.gui.views.listeners.ValueListener;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Fabien Barbero
 */
public class PaginatedPanel<T> extends JPanel {
    
    private final List<ValueListener<Integer>> pageIndexListeners = new ArrayList<ValueListener<Integer>>();
    private final List<ValueListener<T>> selectionListeners = new ArrayList<ValueListener<T>>();
    
    private final JPanel navPanel;
    
    private final ThumbnailsList list;
    
    public PaginatedPanel() {
        super(new BorderLayout());
        
        list = new ThumbnailsList();
        add(new JScrollPane(list), BorderLayout.CENTER);
        
        navPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 7, 0));
        add(navPanel, BorderLayout.SOUTH);
        
        list.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent evt) {
                if(evt.getValueIsAdjusting()) {
                    fireSelectionListeners((T) list.getSelectedValue());
                }
            }
            
        });
    }
    
    public void setListRenderer(ListCellRenderer renderer) {
        list.setCellRenderer(renderer);
    }
    
    public synchronized void setPaginated(Paginated<T> paginated) {
        navPanel.removeAll();
        list.getModel().removeAllElements();
        for(T element : paginated.getValues()) {
            list.getModel().addElement(element);
        }
    }
    
    public synchronized void addSelectionListener(ValueListener<T> listener) {
        selectionListeners.add(listener);
    }
    
    public synchronized void removeSelectionListener(ValueListener<T> listener) {
        selectionListeners.remove(listener);
    }
    
    private synchronized void fireSelectionListeners(T value) {
        for(ValueListener<T> l : selectionListeners) {
            l.valueChanged(value);
        }
    }
    
    public synchronized void addPageIndexListener(ValueListener<Integer> listener) {
        pageIndexListeners.add(listener);
    }
    
    public synchronized void removePageIndexListener(ValueListener<Integer> listener) {
        pageIndexListeners.remove(listener);
    }
    
    private synchronized void firePageIndexListeners(int pageIndex) {
        for(ValueListener<Integer> l : pageIndexListeners) {
            l.valueChanged(pageIndex);
        }
    }
    
}
